package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Person;
import com.ruoyi.system.domain.Ptesting;
import com.ruoyi.system.domain.vo.PtestingVo;
import com.ruoyi.system.mapper.PersonMapper;
import com.ruoyi.system.mapper.PtestingMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.IPtestingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 核酸信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Service
public class PtestingServiceImpl implements IPtestingService {
    @Autowired
    private PtestingMapper ptestingMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private PersonMapper personMapper;

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    /**
     * 查询核酸信息
     *
     * @param id 核酸信息主键
     * @return 核酸信息
     */
    @Override
    public Ptesting selectPtestingById(Long id) {
        return ptestingMapper.selectPtestingById(id);
    }

    /**
     * 查询核酸信息列表
     *
     * @param ptesting 核酸信息
     * @return 核酸信息
     */
    @Override
    @DataScope(deptAlias = "p")
    public List<Ptesting> selectPtestingList(Ptesting ptesting) {
        List<Ptesting> ptestings = ptestingMapper.selectPtestingList(ptesting);
        for (Ptesting ptesting1 : ptestings) {
            ptesting1.setDeptName(gitDept(ptesting1.getDeptId()));
        }
        return ptestings;
    }

    public String gitDept(Long dept) {
        String deptName = "";
        String[] split = ((sysDeptMapper.selectDeptById(dept).getAncestors()) + "," + dept).split(",");
        for (int a = 1; a < split.length; a++) {
            deptName += sysDeptMapper.selectDeptById(Long.valueOf(split[a])).getDeptName() + "-";
        }
        return deptName.substring(0, deptName.length() - 1);
    }

    /**
     * 查询核酸信息列表
     *
     * @param ptesting 核酸信息
     * @return 核酸信息
     */
    @Override
    public Integer selectPtestingCount(Ptesting ptesting) {
        return ptestingMapper.selectPtestingCount(ptesting);
    }


    /**
     * 查询未检测的核酸信息列表
     *
     * @param ptesting 核酸信息
     * @return 核酸信息
     */
    @Override
    @DataScope(deptAlias = "c")
    public List<Map> selectNotPtestion(Ptesting ptesting) {
        return ptestingMapper.selectNotPtestion(ptesting);
    }

    /**
     * 查询未检测的核酸信息列表
     *
     * @param ptesting 核酸信息
     * @return 核酸信息
     */
    @Override
    @DataScope(deptAlias = "p")
    public List<Map> selectNotPtestionNo(Ptesting ptesting) {
        return ptestingMapper.selectNotPtestion(ptesting);
    }

    /**
     * 查询未检测的核酸信息列表
     *
     * @param ptesting 核酸信息
     * @return 核酸信息
     */
    @Override
    @DataScope(deptAlias = "P")
    public List<Map> selectNotPtestionZONG(Ptesting ptesting) {
        return ptestingMapper.selectNotPtestionZONG(ptesting);
    }

    /**
     * 新增核酸信息
     *
     * @param ptesting 核酸信息
     * @return 结果
     */
    @Override
    public int insertPtesting(Ptesting ptesting) {
        ptesting.setCreateTime(DateUtils.getNowDate());
        return ptestingMapper.insertPtesting(ptesting);
    }

    /**
     * 修改核酸信息
     *
     * @param ptesting 核酸信息
     * @return 结果
     */
    @Override
    public int updatePtesting(Ptesting ptesting) {
        return ptestingMapper.updatePtesting(ptesting);
    }

    /**
     * 批量删除核酸信息
     *
     * @param ids 需要删除的核酸信息主键
     * @return 结果
     */
    @Override
    public int deletePtestingByIds(Long[] ids) {
        return ptestingMapper.deletePtestingByIds(ids);
    }

    /**
     * 删除核酸信息信息
     *
     * @param id 核酸信息主键
     * @return 结果
     */
    @Override
    public int deletePtestingById(Long id) {
        return ptestingMapper.deletePtestingById(id);
    }


    /**
     * 导入用户数据
     *
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String imporPerson(List<PtestingVo> ptestingVoList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(ptestingVoList) || ptestingVoList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PtestingVo ptestingVo : ptestingVoList) {
            try {
                List<Person> people = personMapper.selectPersonList(new Person(ptestingVo.getCardId().replaceAll(" ", ""), 0, 0));
                if (people.size() > 0) {
                    //检测时间为空的时候导入不进去，检测状态为空时候也导入不进去
                    if (StringUtils.isNull(ptestingVo.getDetectionTime()) || StringUtils.isNull(ptestingVo.getDetectionResult())) {
                        continue;
                    }
                    Person person = people.get(0);
                    if (person.getDetectionCycleId() != null) {
                        Ptesting ptesting = new Ptesting();
                        ptesting.setPersonId(person.getPersonId()); //重点人群id
                        ptesting.setDetectionTime(ptestingVo.getDetectionTime()); //核酸检测时间
                        ptesting.setDetectionResult(ptestingVo.getDetectionResult().contains("阴") ? 1L : 2L); //阴性阳性
                        ptesting.setPersonName(person.getPersonName());//姓名
                        ptesting.setCardId(person.getCardId()); //身份证号
                        ptesting.setPhoneNumber(person.getPhoneNumber());//手机号
                        ptesting.setDeptId(person.getDeptId());//行业id
                        ptesting.setDetectionCycleId(person.getDetectionCycleId());//类别id
                        ptesting.setCreateTime(new Date());//创建时间
                        ptestingMapper.insertPtesting(ptesting);
                        successNum++;
                    }
                } else {
                    failureNum++;
                    continue;
                }
            } catch (Exception e) {
                failureNum++;
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "导入完成！共 " + failureNum + " 条数据格式不正确,其原因是重点人群中没有该人员身份证号");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
